package com.apache.ius.method;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.DataMap;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * 根据主键删除sysModule表的数据，同时删除其孩子数据。
 */
public class DelSysModuleCasCadeImpl implements CustomMethodPlugin {

    protected Logger log = Logger.getLogger(getClass());

    @Override
    public Object doInvoke(Map<String, Object> param) {

        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 级联删除
     * params：
     *
     * @see CustomMethodPlugin#beforeInvoke(java.util.Map)
     */
    @Override
    public Object beforeInvoke(Map<String, Object> param) {

        final String sysModuleId = String.valueOf(param.get("sysModule.w_moduleId"));
        if (Validator.isNotNull(sysModuleId)) {
            new Thread(new Runnable() {

                public void run() {
                    ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
                            .getBean("iusProxyManager");
                    getChild(sysModuleId, netProxyManager, null, "");
                }
            }).start();

        }
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {

        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

    private void delSysModule(String sysModuleId, ProxyManager netProxyManager) {
        ParamsVo delParams = new ParamsVo();
        delParams.setParams("doCode", "dymicSql");
        delParams.setKey("ByDymicSql");
        delParams.setParams("resultType", "processSql");
        delParams.setKey("publicForSql");
        //封装参数
        delParams.setParams("modelTypes", "d_sysModule");
        delParams.setParams("sysModule.w_moduleId", sysModuleId);
        ResultEntity delResult = (ResultEntity) netProxyManager.doInvoke(delParams);
        log.info("删除主键值为" + delResult.getEntity() + "的数据成功");
    }

    private void getChild(String sysModuleId, ProxyManager netProxyManager, List<DataMap> list,
            String jdbcType) {

        ParamsVo params = new ParamsVo();
        params.setParams("doCode", "dymicSql");
        params.setParams("resultType", "objInfo");
        params.setKey("publicForSql");
        //封装参数
        params.setParams("modelTypes", "s_sysModule");
        params.setParams("sysModule.w_fatherId", sysModuleId);
        ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        if (dymicSqlParamResult != null) {
            List<DataMap> sysModuleList = (List) dymicSqlParamResult.getEntity();
            if (!Validator.isEmpty(sysModuleList) && sysModuleList.size() > 0) {
                for (int i = 0; i < sysModuleList.size(); i++) {
                    DataMap maps = sysModuleList.get(i);
                    delSysModule(maps.get("moduleId").toString(), netProxyManager);
                    //list.add(sysModuleList.get(i));
                    getChild(maps.get("moduleId").toString(), netProxyManager, list, jdbcType);
                }
            }
            //String primaryKey = String.valueOf(dataMap.get("moduleId"));
        }
    }

}
